
package com.naza.rpc.filter.support;

import com.naza.rpc.filter.Filter;
import com.naza.rpc.ratelimit.IntervalRateLimiter;
import com.naza.rpc.ratelimit.RateLimiter;
import com.naza.rpc.ratelimit.SemaRatelimiter;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

/**
 * 服务端流控过滤器
 * @author yl
 */
public class RateLimiterFilter implements Filter {

    private RateLimiter limiter;

    public RateLimiterFilter(){
        limiter = new IntervalRateLimiter(100L);
    }

    @Override
    public boolean before(Method method, Object processor, Object[] requestObjects) {
        Long result = limiter.tryAcquire();
        return result == 1L?true:false;
    }

    @Override
    public void after(Method method, Object processor, Object[] requestObjects) {
        limiter.release();
    }
}

